class CompanyListController {
    constructor($rootScope, $scope, $cookieStore, $timeout, $state, ngDialog, dialogService, getCompanyList, iRunner){

        this.$rootScope     = $rootScope;
        this.$cookieStore   = $cookieStore;
        this.$timeout       = $timeout;
        this.$state         = $state;
        this.ngDialog       = ngDialog;
        this.dialogService  = dialogService;
        this.getCompanyList = getCompanyList;
        this.iRunner        = iRunner;

        // 机构类型
        this.company_types = iRunner.raceScaleConfig;

        // 判断移动端屏幕宽度
        this.isMobile = window.screen.width < 768;

        // 过滤
        this.query = '';

        // 空数组
        this.companylist = [];

        // 获取赛事机构列表
        (this.getCompanyList.code==0) && this.search('');
        
        console.log(this.companylist);

        // 判断数据是否为空, 提示用户
        if(this.getCompanyList.data.length==0){
            this.$timeout( () => {
                $('#grumble').grumble({
                    text: '这里可以创建赛事机构',
                    angle: 60,
                    distance: 50,
                    showAfter: 0,
                    hideAfter: false,
                    hasHideButton: true,
                    buttonHideText: 'Pop!'
                })
            }, 1000)
        }

    }

    // 过滤数组
    search(query) {

        // 判断是否为空
        if (this.getCompanyList.data.length==0) {
            return false;
        }

        // 置空数组
        this.companylist = [];

        // 处理数组
        angular.forEach(this.getCompanyList.data, (value, index) => {
            if(value.company_name.indexOf(query)!=-1){
                // value['race_url']  = 'http://' + location.host + '/web/race/index?company=' + value.company_id;
                //web/company/index?id=31(张榕)
                value['race_url']  = 'http://' + location.host + '/web/company/index?id=' + value.company_id;

                value['qrcode_id'] = 'qrcode-' + value.company_id;
                this.$timeout( () => {
                    if ($('#qrcode-' + value.company_id).find('canvas').length==0) {
                        $('#qrcode-' + value.company_id).qrcode({
                            width: 80,
                            height: 80,
                            text: value['race_url']
                        });
                    }
                }, 0)
                this.companylist.push(value)
            }
        })

    }

    // 点击进入赛事列表
    list(status, company_id) {

        // 未认证机构，提示用户
        if (status=='verify') {
            this.ngDialog.open({
                template: `<p style="margin-top: 30px">您的赛事机构申请已受理，</p>
                        <span>工作人员将会在三个工作日内联系您。</span>`,
                className: 'ngdialog-theme-default ngdialog-alert',
                plain: true
            })
            return false;
        }

        // 已认证机构，跳转至赛事列表
        if (status=='normal') {
            this.$state.go('dashboard.list', { company_id: company_id })
        }
    }

}

CompanyListController.$inject = ['$rootScope', '$scope', '$cookieStore', '$timeout', '$state', 'ngDialog', 'dialogService', 'getCompanyList', 'iRunner']; 

export default CompanyListController;